package com.kaihong.template.ble.operation.impl.subscriber;

import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.bluetooth.BluetoothHost;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;

import com.kaihong.template.ble.operation.callback.adapter.BluetoothAdapterStateChangeCallback;
import com.kaihong.template.utils.Logger;


/**
 * Subscriber BluetoothAdapter State
 */
public class BluetoothAdapterStateSubscriber extends CommonEventSubscriber {
    private static final String TAG = "BluetoothAdapterStateSubscriber";

    private static final int STATE_OFF = 10;

    private static final int STATE_TURNING_ON = 11;

    private static final int STATE_ON = 12;

    private static final int STATE_TURNING_OFF = 13;

    private BluetoothAdapterStateChangeCallback bluetoothAdapterStateChangeCallback;

    private boolean isDiscovering;

    /**
     * constructor with subscribeInfo
     *
     * @param subscribeInfo subscribeInfo
     */
    public BluetoothAdapterStateSubscriber(CommonEventSubscribeInfo subscribeInfo) {
        super(subscribeInfo);
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        if (commonEventData == null) {
            return;
        }
        Intent info = commonEventData.getIntent();
        if (info == null) {
            return;
        }
        if (bluetoothAdapterStateChangeCallback == null) {
            return;
        }
        dealWithActions(info);
    }

    /**
     * set callback of bluetooth adapter state change
     *
     * @param bluetoothAdapterStateChangeCallback callback
     */
    public void setBluetoothAdapterStateChangeCallback(
        BluetoothAdapterStateChangeCallback bluetoothAdapterStateChangeCallback) {
        this.bluetoothAdapterStateChangeCallback = bluetoothAdapterStateChangeCallback;
    }

    private void dealWithActions(Intent info) {
        try {
            if (info != null) {
                IntentParams myParam = info.getParams();
                if (myParam != null && !myParam.isEmpty()) {
                    int currentState = getBluetoothState((int) myParam.getParam(BluetoothHost.HOST_PARAM_CUR_STATE));

                    // Enabling or disabling Bluetooth, not reported
                    if (currentState != BluetoothHost.STATE_TURNING_ON && currentState != BluetoothHost.STATE_TURNING_OFF) {
                        boolean isAvailable = currentState == BluetoothHost.STATE_ON;
                        switch (info.getAction()) {
                            case BluetoothHost.EVENT_HOST_STATE_UPDATE:
                                Logger.i(
                                    TAG,
                                    "BluetoothAdapterStateSubscriber onReceiveEvent : "
                                        + myParam.getParam(BluetoothHost.HOST_PARAM_CUR_STATE));
                                Logger.i(TAG, "BluetoothAdapterStateSubscriber currentState : " + isAvailable);
                                if (bluetoothAdapterStateChangeCallback != null) {
                                    bluetoothAdapterStateChangeCallback.onBluetoothAdapterStateChange(isDiscovering, isAvailable);
                                }
                                break;
                            case BluetoothHost.EVENT_HOST_DISCOVERY_STARTED:
                                Logger.i(TAG, "BluetoothAdapterStateSubscriber EVENT_HOST_DISCOVERY_STARTED");
                                isDiscovering = true;
                                if (bluetoothAdapterStateChangeCallback != null) {
                                    bluetoothAdapterStateChangeCallback.onBluetoothAdapterStateChange(isDiscovering, isAvailable);
                                }
                                break;
                            case BluetoothHost.EVENT_HOST_DISCOVERY_FINISHED:
                                Logger.i(TAG, "BluetoothAdapterStateSubscriber EVENT_HOST_DISCOVERY_FINISHED");
                                isDiscovering = false;
                                if (bluetoothAdapterStateChangeCallback != null) {
                                    bluetoothAdapterStateChangeCallback.onBluetoothAdapterStateChange(isDiscovering, isAvailable);
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private int getBluetoothState(int state) {
        switch (state) {
            case STATE_OFF:
                return BluetoothHost.STATE_OFF;
            case STATE_TURNING_ON:
                return BluetoothHost.STATE_TURNING_ON;
            case STATE_ON:
                return BluetoothHost.STATE_ON;
            case STATE_TURNING_OFF:
                return BluetoothHost.STATE_TURNING_OFF;
            default:
                return state;
        }
    }
}
